<template>
  <section class="QuickCalendarMonth__container">
    <div class="title">
      <span class="month">{{ month }}</span>
      <span class="year">{{ year }}</span>
    </div>
    <div class="weekday">
      <span>一</span>
      <span>二</span>
      <span>三</span>
      <span>四</span>
      <span>五</span>
      <span>六</span>
      <span>七</span>
    </div>
    <div class="box">
      <div v-for="(item, index) in items" :key="index" class="item">
        <span v-if="item.date > 0" class="panel">
          <h1 class="date">
            {{ item.date }}
            <slot name="status-bar" :item="item.data"> </slot>
          </h1>
          <slot name="content" :item="item.data"> </slot>
        </span>
        <span v-else class="nodate"></span>
      </div>
    </div>
  </section>
</template>

<script>
import moment from "moment";

export default {
  components: {},
  props: {
    year: {
      type: Number,
    },

    month: {
      type: Number,
    },

    data: {
      type: Array,
    },
  },

  computed: {
    items() {
      const days = [];

      if (true) {
        switch (this.month) {
          case 1:
          case 3:
          case 5:
          case 7:
          case 8:
          case 10:
          case 12:
            for (let i = 1; i <= 31; i++) {
              const date = new Date(`${this.year}-${this.month}-${i}`);

              days.push({
                fullDate: moment(date).format("yyyy-MM-DD"),
                date: i,
                col: date.getDay() == 0 ? 7 : date.getDay(),
              });
            }
            break;
          case 4:
          case 6:
          case 9:
          case 11:
            for (let i = 1; i <= 30; i++) {
              const date = new Date(`${this.year}-${this.month}-${i}`);

              days.push({
                fullDate: moment(date).format("yyyy-MM-DD"),
                date: i,
                col: date.getDay() == 0 ? 7 : date.getDay(),
              });
            }
            break;
          case 2:
            for (let i = 1; i <= 28; i++) {
              const date = new Date(`${this.year}-${this.month}-${i}`);

              days.push({
                fullDate: moment(date).format("yyyy-MM-DD"),
                date: i,
                col: date.getDay() == 0 ? 7 : date.getDay(),
              });
            }
          default:
            break;
        }
      }

      let col = days[0].col;
      while (col > 1) {
        col--;
        days.unshift({
          date: -1,
          col: col,
        });
      }

      col = days[days.length - 1].col;
      while (col < 7) {
        col++;
        days.push({
          date: -1,
          col: col,
        });
      }

      this.data.map((m) => {
        const found = days.find((d) => m.date == d.fullDate);
        if (found) {
          found.data = m;
        }
      });

      console.log("days", days);

      return days;
    },
  },

  methods: {
    click(data) {
      this.$emit("click", data);
    },
  },
};
</script>

<style lang="scss" scoped>
.QuickCalendarMonth__container {
  display: flex;
  flex-direction: column;
  margin: 0 0 30px 0;
  overflow: hidden;

  .title {
    background: linear-gradient(45deg, coral, 20%, white 0);
    padding: 10px 0;
    padding-left: 30px;
    color: white;
    border-bottom: 1px solid coral;
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;

    display: flex;
    flex-direction: column;
    justify-content: center;

    .year {
      font-size: 1.5rem;
      margin-top: 5px;
      letter-spacing: 3px;

      &::after {
        content: "年";
        font-size: 1.2rem;
      }
    }

    .month {
      font-size: 3rem;

      &::after {
        content: "月";
        font-size: 1.4rem;
      }
    }
  }

  .weekday {
    display: flex;
    width: 800px;
    justify-content: space-evenly;

    span {
      font-size: 2rem;
      width: 100px;
      height: 60px;
      padding: 5px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }

  .box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
    width: 800px;

    .item {
      width: 100px;
      font-size: 1.6rem;
      font-weight: bold;
      padding: 5px;
      margin-bottom: 10px;

      .panel {
        display: flex;
        flex-direction: column;
        overflow: hidden;

        .date {
          display: flex;
          justify-content: space-between;
          align-items: center;
          height: 30px;
          color: #666;
          padding: 0 5px;
        }
      }
    }
  }
}
</style>